const BaseService = require("./base_service.js");
const UserModel = require('../model/user_model.js');
const dataUtil = require('../../../framework/utils/data_util.js');
const cloudBase = require('../../../framework/cloud/cloud_base.js');

class UserService extends BaseService {

    // 注册
    async phone_register(openId, { cloudID, spread_openid }) {
        let where = {
            'user_openid': openId
        };
        let userInfo = await UserModel.getOne(where);
        if (!userInfo) {
            // 手机注册
            let cloud = cloudBase.getCloud();
            const apiData = await cloud.getOpenData({
                "list": [cloudID]
            })
            if (apiData.list[0].data.phoneNumber) {
                let phoneNumber = apiData.list[0].data.phoneNumber;
                // 入库
                let data = {
                    user_openid: openId,
                    user_phone: phoneNumber,
                    spread_openid: spread_openid || ""
                }
                await UserModel.insert(data);
            }
        }
        let res = await this.get_info(openId);
        return res;
    }

    // 获取用户信息
    async get_info(openId) {
        let res = {
            open_id: '',
            nickname: '',
            realname: '',
            phone: '',
            avatar: '',
            sex: '',
            is_admin: '',
        };
        let where = {
            user_openid: openId
        }
        let userInfo = await UserModel.getOne(where);
        if (userInfo) {
            // 数据整理
            res.open_id = userInfo.user_openid,
            res.nickname = userInfo.user_nickname;
            res.realname = userInfo.user_realname;
            res.phone = userInfo.user_phone;
            res.avatar = userInfo.user_avatar;
            res.sex = userInfo.user_sex;
            res.is_admin = userInfo.user_is_admin;
            // 更新登陆时间
            let dataUpdate = {
                user_login_time: this._timestamp
            };
            UserModel.edit(where, dataUpdate);
        }
        return res;
    }

    // 编辑用户信息
    async edit_info(openId, {
        nickname,
        realname,
        phone,
        avatar,
        sex
    }) {
        // 判断是否存在
        let where = {
            user_openid: openId
        }
        let userInfo = await UserModel.getOne(where);
        let data = {
            user_openid: openId,
            user_phone: phone,
            user_nickname: nickname,
            user_realname: realname,
            user_avatar: avatar,
            user_sex: sex
        }
        if (userInfo) {
            // 修改
            await UserModel.edit(where, data);
        } else {
            // 新增   
            await UserModel.insert(data);
        }
        return { status: 1 };
    }

    // 记录分享朋友圈时间
    async record_share_moments_time(openId) {
        const userInfo = await this.getUserInfo(openId);
        const nowTime = this._timestamp;
        let where = {
            user_openid: openId
        }
        let data = {
            share_moments_new_time: nowTime
        }
        if (!userInfo.share_moments_first_time) {
            data.share_moments_first_time = nowTime;
        }
        await UserModel.edit(where, data);
        return { status: 1 };
    }
}

module.exports = UserService;